let watermark = {}

let setWatermark = (str, fontSize) => {
    let id = '1.23452384164.123412415'

    if (document.getElementById(id) !== null) {
        document.body.removeChild(document.getElementById(id))
    }

    let canvas = document.createElement('canvas')
    canvas.width = 180;// 稠密度
    canvas.height = 120;

    let context = canvas.getContext('2d');
    context.rotate(-20 * Math.PI / 180);
    context.font = `${fontSize}px Vedana`;
    context.fillStyle = 'rgba(180, 180, 180, 0.3)';
    context.textAlign = 'left';
    context.textBaseline = 'Middle';
    context.fillText(str, canvas.width / 20, canvas.height);

    let div = document.createElement('div');
    div.id = id;
    div.style.pointerEvents = 'none';
    div.style.top = '3px';
    div.style.left = '-20px';
    div.style.position = 'fixed';
    div.style.zIndex = '100000';
    div.style.width = document.documentElement.clientWidth + 'px';
    div.style.height = document.documentElement.clientHeight + 'px';
    div.style.background = 'url(' + canvas.toDataURL('image/png') + ') left top repeat';
    document.body.appendChild(div);
    return id;
}

// 该方法只允许调用一次
watermark.set = (str, fontSize = 18) => {
    setWatermark(str, fontSize);
    window.onresize = () => {
        setWatermark(str, fontSize)
    }
}

export default watermark;